package com.eorchis.module.usermessagemanage.ui.controller;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.ModelBeanUtils;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.usermessagemanage.domain.ExportUserMessageManageBean;
import com.eorchis.module.usermessagemanage.domain.UserMessageManageBean;
import com.eorchis.module.usermessagemanage.domain.UserMessageManageExport;
import com.eorchis.module.usermessagemanage.domain.UserMessageManageSheet;
import com.eorchis.module.usermessagemanage.service.IUserMessageManageService;
import com.eorchis.module.usermessagemanage.ui.commond.UserMessageManageQueryCommond;
import com.eorchis.module.usermessagemanage.ui.commond.UserMessageManageValidCommond;
import com.eorchis.utils.BeanUtils;
import com.eorchis.utils.excelutil.ExcelIO;
import com.eorchis.utils.excelutil.ExcelIOFactory;
import com.eorchis.utils.excelutil.export.bo.Sheet;
import com.eorchis.utils.utils.PropertyUtil;


@Controller("UserMessageManageController")
@RequestMapping(value = UserMessageManageController.MODULE_PATH)
public class UserMessageManageController extends AbstractBaseController<UserMessageManageValidCommond, UserMessageManageQueryCommond> {

	// 模块页面根路径
	public static final String MODULE_PATH = "/module/usermessagemanage";
	
	@Autowired
	@Qualifier("com.eorchis.module.usermessagemanage.service.impl.UserMessageManageServiceImpl")
	private IUserMessageManageService userMessageManageService;
	
	@Override
	public IBaseService getService() {
		return userMessageManageService;
	}
	@RequestMapping(value = "/findUserList")
	public String findUserList(UserMessageManageQueryCommond queryCommond, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("resultState") ResultState resultState) throws Exception{
		Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		queryCommond.setTreepath(d.getTreepath());
		List<UserMessageManageBean> userList = userMessageManageService.findUserList(queryCommond);
		
		queryCommond.setResultList(userList);
		resultState.setMessage("查询成功！");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
		
	}
	@RequestMapping(value = "/preUpdate")
	public String preUpdate(UserMessageManageQueryCommond queryCommond, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("resultState") ResultState resultState,Model model) throws Exception{
		/*Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		queryCommond.setTreepath(d.getTreepath());*/
		if (PropertyUtil.objectNotEmpty(queryCommond.getSearchUsertId())) {
			UserMessageManageBean user = userMessageManageService.getUserMessageManageBean(queryCommond.getSearchUsertId());
			//model.addAttribute("user", user);
			ModelBeanUtils.jsonObjectToModel(model, true, null, user);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else {
			resultState.setMessage("用户id为空!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		
		return "";
		
	}
	@RequestMapping(value = "/updateusermessage")
	public String updateUserMessageManage(UserMessageManageQueryCommond queryCommond, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("resultState") ResultState resultState) throws Exception {
		String heldOfficeDateStr=request.getParameter("heldOfficeDateStr");
		queryCommond.setHeldofficeDate(new SimpleDateFormat("yyyy-MM-dd").parse(heldOfficeDateStr));
		if (PropertyUtil.objectNotEmpty(queryCommond.getUserID())) {
			userMessageManageService.updateUserMessageManage(queryCommond);
			resultState.setMessage("修改成功!");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else {
			resultState.setMessage("传入用户id为空");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			
		}
		
		return "";
	}
	
	
	
	@RequestMapping(value = "/exportExcel")
	public void exportUserMessageManageExcel(UserMessageManageQueryCommond queryCommond, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("resultState") ResultState resultState) throws Exception {
		Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		queryCommond.setTreepath(d.getTreepath());
		queryCommond.setPage(-1);
		List<ExportUserMessageManageBean> userList = userMessageManageService.export(queryCommond);
		
		if (!userList.isEmpty()) {
		
			//List<ExportUserMessageManageBean> exportUserMessageManageBeanList = new ArrayList<ExportUserMessageManageBean>();
			/*for (UserMessageManageBean UserMessageManageBean : userList) {
				ExportUserMessageManageBean eub = new ExportUserMessageManageBean();
				BeanUtils.copyProperties(UserMessageManageBean, eub);
				exportUserMessageManageBeanList.add(eub);
			}*/
			UserMessageManageExport userMessageManageExport = new UserMessageManageExport();
			userMessageManageExport.setExportUserMessageManageBeanList(userList);
			UserMessageManageSheet ummSheet = new UserMessageManageSheet();
			ummSheet.setExportObject(userMessageManageExport);
			ummSheet.setExprotFormatName("userMessageManageListExportConfig");
			ummSheet.setSheetName("西城区用户列表");
			
			Sheet[] sheetArray = new Sheet[] { ummSheet };// 导出Excel的Sheet集合
			ExcelIO excelio = ExcelIOFactory.getExcelIO();// Excel导出工具
			String title = "西城区用户列表";
			response.setContentType("application/msexcel;charset=ISO8859-1");
			response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(title, "UTF-8") + ".xls");
			OutputStream out = response.getOutputStream();
			excelio.exportExcel(out, "resumeExportStyle", sheetArray, false);
		} else {
			response.setCharacterEncoding("GB2312");
			PrintWriter pw = response.getWriter();
			pw.write("<script>alert('暂无数据');window.opener='';window.open('', '_self', '');window.close();</script>");
			pw.close();
		}
	}
	
	
}
